/**
 * 分页控件 params:pageSize,pageNum,total,getUrl，params
 */
$.fn.pagination = function(options) {
	var obj = this;
	var defaults = {
		pageSize : 10,
		pageNum:1,
		total:null,
		//onPage:$.noop,
		//PageItems:[5,10,20,30,50,100]
	};
	var opt = $.extend(defaults, options);
	// 当前页面
	var currentPage = Math.max.apply(Math, [ 1, opt.pageNum ]);
	// 总页面
	var maxPage = Math.ceil(opt.total / opt.pageSize);
	// 当前页面比总页面小
	currentPage = Math.min.apply(Math, [ currentPage, maxPage ]);
	//开始位置
	var startIndex = Math.min.apply(Math, [Math.max.apply(Math, [ (currentPage - 2) * opt.pageSize + 1, 0 ]),opt.total ]);// 开始位置
	//结束位置
	var endIndex = Math.min.apply(Math, [ (parseInt(startIndex-1) + parseInt(opt.pageSize)), opt.total ]);// 到的位置
	
	var beginPage = 1;
	var prePage = Math.max.apply(Math, [ currentPage - 2, beginPage ]);
	var nextPage = Math.min.apply(Math, [ currentPage + 8, maxPage ]);
	///////////////
	
	
	$(this).html('<div class="row"><div class="col-md-6"></div><div class="col-md-6"><nav><ul class="pagination"></ul></nav></div></div>');
	var _pager = $(this).find('.row .pagination');
	//开始
	_pager.append('<li><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>');
	//。。。前
	var printPage=prePage;
	for(;printPage<=prePage+2;printPage++){
		if(printPage>nextPage){
			break;
		}
		if(printPage==currentPage){
			_pager.append('<li class="active"><a href="#">'+printPage+'</a></li>');
		}else{
			_pager.append('<li><a href="#">'+printPage+'</a></li>');
		}
	}
	//...
	
	if(printPage<=nextPage-3){
		_pager.append('<li><a href="#">...</a></li>');
	}
	//...后
	if(printPage<nextPage-2){
		printPage = nextPage-2;
	}
	for(;printPage<=nextPage;printPage++){
		if(printPage==currentPage){
			_pager.append('<li class="active"><a href="#">'+printPage+'</a></li>');
		}else{
			_pager.append('<li><a href="#">'+printPage+'</a></li>');
		}
	}
	_pager.append('<li><a href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>');
	// 点击事件加载
	$(this).find('span.link').click(function() {
		if (opt.getUrl) {
			var baseUrl = opt.getUrl();
			var index = $(this).attr('index');
			window.location.href = obj.formUrl(baseUrl,index);
		}
	});
	$(this).find('span.fresh').click(function(){
		if (opt.getUrl) {
			var baseUrl = opt.getUrl();
			window.location.href = obj.formUrl(baseUrl,currentPage);
		}
	});
	var pageCache=1;
	$(this).find('.current-page-input').keyup(function(){
		pageCache=$(this).val();
	});
	$(this).find('.current-page-input').keyup(function(){
		   var val=$(this).val();
			var c=parseInt(val);
			if(c==null || c=='NaN'){
				val=pageCache;
			}
			val=val>=maxPage?maxPage:val;
			var baseUrl = opt.getUrl();
			window.location.href = obj.formUrl(baseUrl,val);			

		
	});
	$(this).find(".page-pageSizeItems").change(function(){
		if (opt.getUrl) {
			opt.pageSize=$(this).val();
			var baseUrl = opt.getUrl();
			window.location.href = obj.formUrl(baseUrl,startIndex);
		}		
	});
	/**
	 * 封装url
	 */
	this.formUrl = function(baseUrl,index){
		if (baseUrl.indexOf('?') <= 0) {
			retUrl = baseUrl + '?';
		} else {
			retUrl = baseUrl + '&';
		}
		retUrl = retUrl + 'pager.pageSize=' + opt.pageSize
				+ '&pager.pageNo=' + index + '&pager.totalRows='
				+ opt.total;
		retUrl = retUrl + '&_data=' + new Date().getTime();
		if( options.getParams ){
			var params = options.getParams() || {};
			var paramsStringUrl='';
			for ( var _index in params) {
				var o=params[_index];
				paramsStringUrl +='&' + o.name + '=' + encodeURI(encodeURI(o.value));
			}
			if(paramsStringUrl!=''){
				retUrl+= paramsStringUrl;
			}
		}
		return retUrl;
	}
	this.search=function(){
		if (opt.getUrl) {
			var baseUrl = opt.getUrl();
			window.location.href = obj.formUrl(baseUrl,1);
		}
	}
	return this;
};